package edu.hawaii.myisern.action;

import static org.junit.Assert.assertEquals;
import org.junit.Test;
import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebForm;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;

/**
 * Provides test cases for MyIsernActionBean.
 * 
 * @author Jon Lao
 * @author Jared Sunouchi
 * @author Chiao-Fen Zielinski-Liu
 */
public class TestMyIsernActionBean {

  /** Get the test host. */
  private String testHost = System.getProperty("test.host");
  
  /** The page title. */
  private String displayTitle = "MyISERN 1.3 - Display";
  
  private String username = "username";
  private String password = "password";
  
  
  /**
   * Tests the IndexActionBean class.
   *
   * @throws Exception If problems occur
   */
  @Test
  public void testMyIsernActionBean() throws Exception {
    // prevent javascript errors from being thrown
    com.meterware.httpunit.HttpUnitOptions.setExceptionsThrownOnScriptError (false);
    com.meterware.httpunit.HttpUnitOptions.setExceptionsThrownOnErrorStatus(false);    
    com.meterware.httpunit.HttpUnitOptions.setScriptingEnabled(false);
    
    WebConversation conversation = new WebConversation();
    String url = testHost + "myisern-1-green/index.jsp";
    WebResponse response = conversation.getResponse(url);
    assertEquals("Checking index.jsp retrieval", "MyISERN 1.3 Login", response.getTitle());
    
    WebForm loginForm = response.getFormWithID("LoginForm");
    WebRequest loginRequest = loginForm.getRequest();
    response = conversation.getResponse(loginRequest);
    loginRequest = loginForm.getRequest();
    
    loginRequest.setParameter(username, username);
    loginRequest.setParameter(password, password);
    
    response = conversation.getResponse(loginRequest);
    
    url = testHost + "myisern-1-green/display.jsp";
    response = conversation.getResponse(url);
    assertEquals("Checking add.jsp retrieval", displayTitle, response.getTitle());
    
    WebForm collaborationForm = response.getFormWithID("CollaborationForm");
    WebRequest collaborationRequest = collaborationForm.getRequest();
    response = conversation.getResponse(collaborationRequest);
    assertEquals("Checking Collaboration.action retrieval", displayTitle, response.getTitle());
    
    url = testHost + "myisern-1-green/display.jsp";
    response = conversation.getResponse(url);
    assertEquals("Checking add.jsp retrieval", displayTitle, response.getTitle());
    
    WebForm organizationForm = response.getFormWithID("OrganizationForm");
    WebRequest organizationRequest = organizationForm.getRequest();
    response = conversation.getResponse(organizationRequest);
    assertEquals("Checking Collaboration.action retrieval", displayTitle, response.getTitle());
    
    url = testHost + "myisern-1-green/display.jsp";
    response = conversation.getResponse(url);
    assertEquals("Checking add.jsp retrieval", displayTitle, response.getTitle());
    
    WebForm researcherForm = response.getFormWithID("ResearcherForm");
    WebRequest researcherRequest = researcherForm.getRequest();
    response = conversation.getResponse(researcherRequest);
    assertEquals("Checking Collaboration.action retrieval", displayTitle, response.getTitle());
  }
}
